package ldh.im.fxbase.data.pojo.status;

import ldh.rpc.dto.ValueEnum;
import ldh.im.dto.enums.MessageStatus;

public enum ImMessageStatus implements ValueEnum<Byte>{
    none(0, "空"),
    // 发送端状态
    created(1, "已创建"),      // 已经创建
    sended(3, "已发送"),       // 发送到服务器，并返回成功

    readed(5, "已阅读"),       // 消息已阅读

    delete(9, "删除"),         // 删除, 仅仅删除本地消息

    withdraw(11, "撤回"),      // 撤回
    withdrawed(12, "已撤回"),  // 撤回

    updated(20, "已更新"),

    disable(30, "不可用"),      // 无用

    serverDeleted(50, "已删除"),

    ref(60, "引用消息"),        // 被引用的消息不展示

    failure(-100, "发送失败"),
    sending(-10, "发送中"),
    ;

    private Byte value;
    private String desc;

    private ImMessageStatus(Integer value, String desc){
        this.value = value.byteValue();
        this.desc = desc;
    }

    @Override
    public Byte getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }

    public static ImMessageStatus wrap(MessageStatus messageStatus, boolean isSender) {
        if (messageStatus == MessageStatus.receive) {
            return isSender ? sended : sended;
        } else if (messageStatus == MessageStatus.readed) {
            return isSender ? sended : sended;
        } else if (messageStatus == MessageStatus.withdrawed) {
            return isSender ? withdrawed : withdrawed;
        } else if (messageStatus == MessageStatus.delete) {
            return isSender ? serverDeleted : serverDeleted;
        } else if (messageStatus == MessageStatus.updated) {
            return isSender ? updated : updated;
        } else if (messageStatus == MessageStatus.none) {
            return isSender ? none : none;
        }
        throw new RuntimeException("ImMessageStatus不支持这种类型MessageStatus(" + messageStatus.name() + ")");
    }

    public static ImMessageStatus getByValue(Integer value) {
        if (value == null) return null;
        for (ImMessageStatus messageStatus : ImMessageStatus.values()) {
            if (messageStatus.getValue() == value.byteValue()) {
                return messageStatus;
            }
        }

        throw new RuntimeException("ImMessageStatus不支持这个值：" + value);
    }

    public boolean isWithdraw() {
        return this == withdraw || this == withdrawed;
    }
}
